<template>
  <zc-card>
    <avue-crud
      ref="crud"
      :option="option"
      :page.sync="page"
      :table-loading="loading"
      @on-load="getList"
      @row-update="rowUpdate"
      :upload-after="uploadAfter"
      @row-save="rowSave"
      @row-del="rowDel"
      @refresh-change="refreshChange"
      @search-reset="searchChange"
      @search-change="searchChange"
      v-model="form"
      :data="data"
      :permission="permission"
    >
      <template slot-scope="scope" slot="idForm">
        <tinymce
          :disabled="scope.disabled"
          v-model="scope.row.goods_info"
          :height="200"
        />
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-edit"
          :type="scope.type"
          :size="scope.size"
          @click.stop="handleEdit(scope.row, scope.index)"
          v-if="permission.editBtn"
          >编辑</el-button
        >
      </template>
      <template slot="img" slot-scope="scope">
        <div class="user">
          <img
            class="column-img-avatar"
            :src="scope.row.img"
            @click="$ImagePreview([{ url: scope.row.img }], 0)"
          />
          <div class="user-msg">
            <div class="namex">{{ scope.row.nickname }}</div>
            <div>{{ scope.row.mobile }}</div>
          </div>
        </div>
      </template>
      <template slot="state" slot-scope="scope">
        <el-switch
          @change="
            (e) => {
              switchChange(e, scope.row.id);
            }
          "
          v-model="scope.row.state"
          :active-value="1"
          :inactive-value="2"
          active-color="#1890ff"
          inactive-color="#DCDFE6"
        ></el-switch>
      </template>
      <template slot="is_distribution" slot-scope="scope">
        <el-switch
          @change="
            (e) => {
              switchChange2(e, scope.row.id);
            }
          "
          v-model="scope.row.is_distribution"
          :active-value="1"
          :inactive-value="2"
          active-color="#1890ff"
          inactive-color="#DCDFE6"
        ></el-switch>
      </template>
      <template slot="is_vip" slot-scope="scope">
        <el-tag v-if="scope.row.is_vip == 1">{{ scope.row.$is_vip }}</el-tag>
        <el-tag type="danger" v-if="scope.row.is_vip == 2">{{
          scope.row.$is_vip
        }}</el-tag>
      </template>
    </avue-crud>
    <zc-dialog
      v-model="obj1"
      :options="option1"
      :showDialog.sync="box99"
      @submit="zc"
    >
    </zc-dialog>
  </zc-card>
</template>

<script>
import ZcDialog from "../admin/zc-dialog.vue";
import { mapGetters } from "vuex";
import {
  getList,
  update,
  add,
  del,
  updateStatus,
  editvipStatus,
} from "@/api/admin/user.js";
import option from "./user-options.js";
import { getToken } from "@/utils/auth";
const defaultSettings = require("../../settings.js");
import Tinymce from "@/components/Tinymce";
export default {
  name: "user",
  components: {
    ZcDialog,
    Tinymce,
  },
  data() {
    return {
      // dialog start
      obj1: {},
      option1: {
        emptyBtn: false,
        submitBtn: false,
        column: [
          {
            label: "用户名",
            prop: "username",
            rules: [
              {
                required: true,
                message: "请输入用户名",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      box99: false,
      // dialog end
      box1: false,
      obj1: {},
      currentRow: {},
      permission: {
        addBtn: false,
        editBtn: false,
        delBtn: false,
        excelBtn: false,
      }, //权限
      page: {
        currentPage: 1,
        pageSize: 10,
      },
      form: {},
      params: {},
      loading: false,
      data: [],
      option: {},
    };
  },
  created() {
    const role = this.$defaultRoleStatus;
    this.permission.addBtn = this.zcbtn[this.$options.name + "_add"] || role;
    this.permission.editBtn = this.zcbtn[this.$options.name + "_edit"] || role;
    this.permission.delBtn = this.zcbtn[this.$options.name + "_del"] || role;
    this.permission.excelBtn =
      this.zcbtn[this.$options.name + "_excel"] || role;
    this.option = option(this);
  },
  mounted() {},
  computed: {
    ...mapGetters(["zcbtn"]),
  },
  methods: {
    zc(v, done) {
      console.log("值", v);
      done();
    },
    // 显示弹窗
    showDialog() {
      this.box99 = false;
      this.box99 = true;
    },
    switchChange2(e, id) {
      if (e == 1) {
        updateStatus({ field: "is_distribution", value: 1, id }).then((res) => {
          this.$notify({
            title: "成功",
            message: "修改状态成功",
            type: "success",
          });
        });
      }
      if (e == 2) {
        updateStatus({ field: "is_distribution", value: 2, id }).then((res) => {
          this.$notify({
            title: "成功",
            message: "修改状态成功",
            type: "success",
          });
        });
      }
    },
    switchChange(e, id) {
      // console.log(id);
      // console.log(e);
      if (e == 1) {
        updateStatus({ field: "state", value: 1, id }).then((res) => {
          this.$notify({
            title: "成功",
            message: "修改状态成功",
            type: "success",
          });
        });
      }
      if (e == 2) {
        updateStatus({ field: "state", value: 2, id }).then((res) => {
          this.$notify({
            title: "成功",
            message: "修改状态成功",
            type: "success",
          });
        });
      }
    },
    // 自定义删除
    handleDel(row, index) {
      this.$refs.crud.rowDel(row, index);
    },
    // 自定义编辑
    handleEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    uploadAfter(res, done) {
      done();
    },
    getList() {
      this.loading = true;
      const data = Object.assign(
        {
          page: this.page.currentPage,
          limit: this.page.pageSize,
        },
        this.params
      );
      this.data = [];

      return getList(data).then((res) => {
        this.loading = false;
        this.page.total = res.count_num;
        this.data = res.data;
        return Promise.resolve(true);
      });
    },
    rowSave(row, done, loading) {
      // return;
      add(
        Object.assign(
          {
            role_id: row.role_name,
          },
          row
        )
      )
        .then(() => {
          this.$message.success("新增成功");
          done();
          this.getList();
        })
        .catch(() => {
          loading();
        });
    },
    rowUpdate(row, index, done, loading) {
      let params = {
        end_vip: row.end_vip,
        ids: row.id,
        content: "",
      };
      if (params.end_vip) {
        params.end_vip = params.end_vip.getTime() / 1000;
      }

      editvipStatus(params)
        .then((res) => {
          this.$message.success("修改成功");
          done();
          this.getList();
        })
        .catch(() => {
          loading();
        });
    },
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return del(row.id);
        })
        .then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
    },
    async searchChange(params, done) {
      // console.log(params);
      if (params.add_time && params.add_time.length > 0) {
        params.start_time = params.add_time[0].getTime() / 1000;
        params.end_time = params.add_time[1].getTime() / 1000;
      }

      if (done) done();
      this.params = params;
      this.page.currentPage = 1;
      await this.getList();
      this.$message.success("搜索成功");
    },
    async refreshChange() {
      await this.getList();
      this.$message.success("刷新成功");
    },
  },
};
</script>

<style lang="scss" scoped>
.column-img-avatar {
  height: 40px;
  width: 40px;
  border-radius: 50%;
}
.user {
  display: flex;
  align-items: center;
  justify-content: center;
}
.user-msg {
  margin-left: 4px;
  text-align: left;
}
.namex {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 110px;
  text-align: left;
}
</style>
